import { request } from '../../utils/request'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 'expend',
    recordData: {},
    page: 1,
    size: 10,
    showNoMore: false,
    showLoading: false,
    dataList: [],
    showSign: false,
    priceVlaue: null,
    beforeClose: {},
    priceList: [0.01, 10, 50, 100, 200, 500],
    selectPrice: 0.01,
    couponList: [],
    couponId: '',
    couponPrice: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      beforeClose: (action) =>
        this.onBeforeClose(action)
    })
  },

  selectCoupon({ currentTarget }) {
    let item = currentTarget.dataset.item
    if (this.data.couponId == item.id) {
      this.setData({
        couponId: '',
        couponPrice: ''
      })
      return
    }
    if (this.data.selectPrice <= item.threshold) {
      wx.showToast({
        title: '优惠券不满足使用条件',
        icon: 'none'
      })
    } else {
      this.setData({
        couponId: item.id,
        couponPrice: item.threshold
      })
    }
  },

  getCouponList() {
    request({
      url: '/mini/api/mine/queryCoupons',
      method: 'POST',
      data: {
        page: 1,
        size: 20,
        status: 0
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          couponList: resdata.data.data
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  selectPrice(item) {
    this.setData({
      selectPrice: item.currentTarget.dataset.item
    })
    if (
      this.data.selectPrice <=
      this.data.couponPrice
    ) {
      this.setData({
        couponId: '',
        couponPrice: ''
      })
    }
  },

  onBeforeClose: function (action) {
    return new Promise((resolve) => {
      if (action === 'confirm') {
        request({
          url: '/mini/api/pay/recharge',
          method: 'POST',
          data: {
            price:
              Number(this.data.selectPrice) * 100,
            productDesc: '充值',
            couponId:
              this.data.couponId > 0
                ? Number(this.data.couponId)
                : ''
          }
        }).then((resdata) => {
          if (resdata.code == 200) {
            wx.requestPayment({
              timeStamp: resdata.data.timeStamp,
              nonceStr: resdata.data.nonceStr,
              package: resdata.data.prepayId,
              signType: resdata.data.signType,
              paySign: resdata.data.paySign,
              success(res) {
                wx.showToast({
                  title: '支付成功',
                  icon: 'none'
                })
                resolve(true)
              },
              fail(res) {
                wx.showToast({
                  title: '支付错误',
                  icon: 'none'
                })
                resolve(false)
              }
            })
          } else {
            wx.showToast({
              title: resdata.message,
              icon: 'none'
            })
            resolve(false)
          }
        })

        // if (
        //   !this.data.priceVlaue ||
        //   this.data.priceVlaue.length <= 0
        // ) {
        //   wx.showToast({
        //     title: '请输入充值金额',
        //     icon: 'none'
        //   })
        //   resolve(false)
        //   return
        // }
        // var dot = this.data.priceVlaue.indexOf(
        //   '.'
        // )
        // if (dot > -1) {
        //   if (
        //     this.data.priceVlaue.length >
        //     dot + 3
        //   ) {
        //     wx.showToast({
        //       title: '只允许输入两位小数',
        //       icon: 'none'
        //     })
        //     resolve(false)
        //   }
        // } else {
        // }
      } else {
        this.onClose()
        resolve(false)
      }
    })
  },

  rechargeNow() {
    this.setData({
      showSign: true
    })
    this.getCouponList()
  },

  onClose() {
    this.setData({
      showSign: false,
      page: 1,
      size: 10
    })
    this.getRecordList()
  },

  getRecordList() {
    request({
      url: '/mini/api/mine/queryAmountRecord',
      method: 'POST',
      data: {
        page: this.data.page,
        size: this.data.size,
        type: this.data.active
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        let newData = this.data.showLoading
          ? this.data.dataList.concat(
              resdata.data.data
            )
          : resdata.data.data
        let showNoMore =
          resdata.data.data.length < 10
        this.setData({
          dataList: newData || [],
          recordData: resdata.data || {},
          showLoading: false,
          showNoMore: showNoMore
        })
      } else {
        this.setData({
          recordData: {},
          dataList: [],
          showLoading: false,
          showNoMore: false
        })
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  onChange(event) {
    this.setData({
      active: event.detail.name
    })
    this.setData({
      page: 1,
      size: 10
    })
    this.getRecordList()
  },

  onReachBottom() {
    if (
      this.data.showLoading ||
      this.data.showNoMore
    ) {
      return
    }
    let newPage = this.data.page + 1
    this.setData({
      showLoading: true,
      page: newPage
    })
    this.getRecordList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getRecordList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})
